//给你二叉树的根节点 root ，返回其节点值的 锯齿形层序遍历 。（即先从左往右，再从右往左进行下一层遍历，以此类推，层与层之间交替进行）。 
//
// 
//
// 示例 1： 
//
// 
//输入：root = [3,9,20,null,null,15,7]
//输出：[[3],[20,9],[15,7]]
// 
//
// 示例 2： 
//
// 
//输入：root = [1]
//输出：[[1]]
// 
//
// 示例 3： 
//
// 
//输入：root = []
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点数目在范围 [0, 2000] 内 
// -100 <= Node.val <= 100 
// 
// Related Topics 树 广度优先搜索 二叉树 👍 691 👎 0


package com.cjl.leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * [P103]_二叉树的锯齿形层序遍历
 * @author cjl
 * @date 2022-09-14 20:30:35
 */
public class P103_BinaryTreeZigzagLevelOrderTraversal{
      public static void main(String[] args) {
            //测试代码
           Solution solution = new P103_BinaryTreeZigzagLevelOrderTraversal().new Solution();
          TreeNode treeNode5 = new TreeNode(7, null, null);
          TreeNode treeNod4 = new TreeNode(15, null, null);
          TreeNode treeNode3 = new TreeNode(20, treeNod4, treeNode5);
          TreeNode treeNode2 = new TreeNode(9, null, null);
          TreeNode treeNode1 = new TreeNode(3, treeNode2, treeNode3);
           solution.zigzagLevelOrder(treeNode1);
      }
      //力扣代码
      //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> resList = new ArrayList<>();
        if(null == root){
            return resList;
        }
        Deque<TreeNode> deque = new ArrayDeque();
        deque.addFirst(root);
        boolean dir = true;
        while(!deque.isEmpty()){
            int size = deque.size();
            List<Integer> oneLeveList= new ArrayList<>();
            for(int i =0 ; i < size; i++){
                if(dir){
                    TreeNode tmp = deque.pollFirst();
                    oneLeveList.add(tmp.val);
                    if(null != tmp.left){
                        deque.addLast(tmp.left);
                    }
                    if(null != tmp.right){
                        deque.addLast(tmp.right);
                    }
                }else{
                    TreeNode tmp = deque.pollLast();
                    oneLeveList.add(tmp.val);
                    if(null != tmp.right){
                        deque.addFirst(tmp.right);
                    }
                    if(null != tmp.left){
                        deque.addFirst(tmp.left);
                    }
                }
            }
            resList.add(oneLeveList);
            dir = !dir;
        }
        return resList;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

  }